package com.youmans.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.youmans.commonutils.R;
import com.youmans.servicebase.exceptionHandler.GuliException;
import com.youmans.vod.service.VodService;
import com.youmans.vod.utils.ConstantVodUtils;
import com.youmans.vod.utils.InitVodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "阿里视频控制器")
@RestController
@RequestMapping("/eduvod/video")
public class VodController {
    @Autowired
    private VodService vodService ;

    @ApiOperation("上传视频文件")
    @PostMapping("uploadAliVideo")
    public R uploadAliVideo(MultipartFile file){
        String videoId = vodService.uploadVideoAli(file) ;
            return R.ok().data("videoId",videoId) ;
    }

    @ApiOperation("根据视频id删除阿里云视频")
    @DeleteMapping("removeAliVideo/{id}")
    public R removeAliVideo(@PathVariable String id){
        try{
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            client.getAcsResponse(request);
            return R.ok() ;
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
    }
    @ApiOperation("根据视频ids删除多个阿里云视频")
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeAliVideos(videoIdList) ;
        return R.ok() ;
    }

    @ApiOperation("根据视频id获取视频凭证")
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id){
        try{
//            创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
//            创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest() ;
//            向request设置视屏id
            request.setVideoId(id);
//            调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request) ;
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        }catch (Exception e){
            throw new GuliException(20001,"获取凭证失败");
        }
    }
}